package org.dromara.capital.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.AccountBookBo;
import org.dromara.admin.domain.bo.ContractBillBo;
import org.dromara.admin.domain.bo.OrderBo;
import org.dromara.admin.domain.vo.ContractBillVo;
import org.dromara.admin.domain.vo.OrderVo;
import org.dromara.admin.service.IContractBillService;
import org.dromara.admin.service.IOrderService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.lessor.service.IHouseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 资方控制器
 *
 * @author Lion Li
 * @date 2025-09-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/capital")
public class CapitalController extends BaseController {

    private final IHouseService houseService;
    private final IOrderService orderService;
    private final IContractBillService contractBillService;

    /**
     * 资方获取我的订单列表
     */
//    @SaCheckPermission("capital:order:list")
    @GetMapping("/myOrderList")
    public TableDataInfo<OrderVo> myOrderList(OrderBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId == null){
            throw new ServiceException("用户未登录");
        }
        return orderService.queryPageMyCapitalOrderList(bo, pageQuery, userId);
    }

    /**
     * 获取订单详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("capital:order:query")
    @GetMapping("/{id}")
    public R<OrderVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable Long id) {
        return R.ok(orderService.queryById(id));
    }

    /**
     * 资方审核是否愿意接单
     */
//    @SaCheckPermission("capital:order:capitalAudit")
    @PutMapping("/capitalAudit")
    public R<Void> capitalAudit(@RequestBody OrderBo bo) {
        Long capitalId = LoginHelper.getUserId();
        if (capitalId == null){
            throw new ServiceException("资方用户未登录");
        }
        return toAjax(orderService.capitalAudit(bo, capitalId));
    }

    /**
     * 资方查询自己未来的收益
     */
//    @SaCheckPermission("capital:order:list")
    @GetMapping("/futureIncomeList")
    public TableDataInfo<ContractBillVo> queryCapitalFutureIncomeList(ContractBillBo bo, PageQuery pageQuery) {
        Long capitalId = LoginHelper.getUserId();
        if (capitalId == null){
            throw new ServiceException("资方用户未登录！");
        }
        bo.setCapitalUserId(capitalId);
        return contractBillService.queryCapitalFutureIncomeList(bo, pageQuery);
    }

}
